<template>
  <div class="m-25 mt-65 agora">
    <div class="normal-box-mod">
      <div class="normal-box-hd clearfix">
        <div class="normal-box-title">
          <div class="decorate-square"></div>
          <h2>人员任务管理</h2>
        </div>
      </div>

      <div class="normal-box-bd">
        <Card style="margin-bottom: 10px;">
            <div class="normal-list-page-hd clearfix">
              <Input placeholder="请输入用户名" class="mr-10" v-model="queryData.userName" style="width: 150px" :clearable=true />
              <Input placeholder="请输入区域名" class="mr-10" v-model="queryData.areaName" style="width: 150px" :clearable=true />
              <Button type="primary" custom-icon="iconfont icon-chaxun" size="small" @click='getTableData' >查询</Button>
            </div>
          </Card>
        <div class="clearfix mb-20">
          <!-- <Button custom-icon="iconfont icon-fangkuai2"
                  type="primary"
                  size="small"
                  style="float:right"
                  @click="showCreateModal">添加</Button> -->
        </div>
        <div class="normal-list-page-mod">
          <Table border
                 :loading="list.loading"
                 :columns="list.columns"
                 :data="list.tableData">

          </Table>

        </div>
        <div class="normal-list-page-ft clearfix">
          <Page class="fr"
                :total="queryData.totalRecord"
                :page-size="queryData.pageSize"
                :current="queryData.page"
                @on-change="changePage"
                @on-page-size-change="onPageSizeChange"
                show-elevator
                show-total
                show-sizer></Page>
        </div>
      </div>
    </div>
    <!-- 新建&修改人员 -->
    <Modal v-model="modal.user.isShow"
           :loading="modal.user.loading"
           :mask-closable="false"
           :title="modal.user.title"
           @on-ok="postData">
      <Form ref="systemFrom"
            :model="modal.user.formData"
            :rules="modal.user.rules"
            :label-width="120">
        <FormItem label="账号"
                  prop="account">
          <Input placeholder="请输入账号"
                 v-model="modal.user.formData.account"
                 style="width:300px;" />
        </FormItem>
      </Form>
    </Modal>

  </div>
</template>
<script>
import UserAreaApi from '@/api/check/voUserArea.js'
import { Message } from 'view-design'

export default {
  data () {
    return {
      queryData: {
        limit: 10,
        page: 1,
        totalRecord: 0
      },
      // 列表
      list: {
        loading: false,
        selections: [],
        // 列表展示数据
        tableData: [],
        // 表格表头
        columns: [
          // {
          //   type: 'selection',
          //   width: 50,
          //   align: 'center'
          // },
          /** 开始自动生成 */
          {
            title: '用户id',
            key: 'uid',
            align: 'center',
            minWidth: 130
          },
          {
            title: '用户名',
            key: 'userName',
            align: 'center',
            minWidth: 130
          },
          {
            title: '区域名称',
            key: 'areaName',
            align: 'center',
            minWidth: 150
          },

          {
            title: '分配时间',
            key: 'createTime',
            align: 'center',
            minWidth: 140
          },

          {
            title: '操作',
            align: 'center',
            minWidth: 100,
            render: (h, params) => {
              return h('div', [
                h(
                  'Button',
                  {
                    props: {
                      type: 'warning',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        this.delData(params.row.id)
                      }
                    }
                  },
                  '删除'
                )
              ])
            }
          }
        ]
      },
      modal: {
        user: {
          isShow: false,
          loading: true,
          type: 'create', // create 新建，update 修改
          title: '添加',
          formData: {},
          rules: {
            // check

          }
        }
      }
    }
  },
  created () {
    this.getTableData()
  },
  methods: {
    // 每页显示记录数变更
    onPageSizeChange (pageSize) {
      this.queryData.limit = pageSize
      this.getTableData()
    },

    // 换页
    changePage (page) {
      this.queryData.page = page
      this.getTableData()
    },
    // 获取列表
    async getTableData () {
      const that = this
      that.queryData.totalRecord = undefined
      UserAreaApi.getPageList(that.queryData)
        .then((res) => {
          that.list.tableData = res.data.list
          that.queryData.totalRecord = res.data.totalRecord
          //   that.queryData.page = res.data.pageNum
          that.queryData.limit = res.data.pageSize
        })
        .catch((error) => {
          Message.error(error)
        })
      //   console.log(this.list)
    },
    showCreateModal () {
      this.modal.user.title = '添加人员'
      this.modal.user.type = 'create'
      this.modal.user.formData = {}
      this.modal.user.isShow = true
    },
    showUpdateModal (row) {
      UserApi.getDetail(row.id).then((res) => {
        this.modal.user.title = '修改人员'
        this.modal.user.type = 'update'
        this.modal.user.formData = res.data
        this.modal.user.isShow = true
      })
    },
    // 新建&修改
    postData () {
      const that = this
      that.$refs['systemFrom'].validate((valid) => {
        if (valid) {
          if (!that.modal.user.formData.id) {
            // 新建
            UserApi.add(that.modal.user.formData)
              .then((res) => {
                that.modal.user.isShow = false
                that.modal.user.loading = false
                if (!res.code) {
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          } else {
            // 修改
            UserApi.update(that.modal.user.formData.id, that.modal.user.formData)
              .then((res) => {
                that.modal.user.isShow = false
                that.modal.user.loading = false
                if (!res.code) {
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          }
        } else {
          that.modal.user.loading = false
          that.$nextTick(() => {
            that.modal.user.loading = true
          })
        }
      })
    },
    // 删除
    delData (id) {
      let that = this
      this.$Modal.confirm({
        title: '确认删除',
        content:
          '<p>您确认删除id为 <strong>' +
          id.toString() +
          '</strong> 的记录吗?</p><p>删除后将无法撤销，请谨慎操作！</p>',
        loading: true,
        onOk: () => {
          UserAreaApi.del({
            ids: [id]
          }).then((res) => {
            this.$Modal.remove()
            if (!res.code) {
              this.$Notice.success({
                title: '删除成功'
              })
              that.getTableData()
            } else {
              this.$Notice.error({
                title: res.message
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
table {
  width: 100%;
  border-collapse: collapse;
  > div {
    width: 100%;
    tr {
      width: 100%;
      vertical-align: middle;
      th {
        font-size: 1.5em;
        background: #f8f8f9;
      }
      td,
      th {
        box-sizing: border-box;
        padding: 5px 0;
        border: 1px solid #e8eaec;
      }
      .c0 {
        font-size: 1.5em;
        width: 4vw;
        background: #fff;
      }
      .c1 {
        font-size: 1.5em;
        width: 12vw !important;
        background: #fdfdfd;
      }
      .c2 {
        width: 20vw;
        font-size: 1.2em;
      }
      .c3 {
        width: 35vw;
        font-size: 1.2em;
      }
      .c4 {
        width: 15vw;
        font-size: 1.5em;
      }
    }
    tr:hover {
      background: #efefef !important;
      transition: all ease 0.5s;
    }
  }
}
</style>
